import { getStorageSync, setStorageSync } from '@tarojs/taro'
import { parseToken } from '_utils'
import { observable } from 'mobx'

const userStore = observable({
  channel: '',
  openId: '',
  userInfo: getStorageSync('userInfo'),
  classInfo: getStorageSync('classInfo'),
  token: '',
  accessTokenExpire: '',
  roleType: getStorageSync('roleType') || 'Teacher',
  organizaIdentity: getStorageSync('organizaIdentity'),
  authH5Code: '', // 公众号授权code
  isDidWxAuthBack: getStorageSync('isDidWxAuthBack'), // 判断是否授权过，在详情页确认用到
  privacyPolicyAgree: false, // 隐私协议是否授权
  subscriptionTemplateList: [], // 教师订阅模版列表
  subscriptionTemplateListEdu: [], // 教育局订阅模版列表
  subscriptionTemplateListStu: [], // 学生订阅模版列表
  uploadGradeList: [], // 学校导入的班级列表
  schoolClassList: [], // 当前学校下 班级列表
  clearClassInfo() {
    this.classInfo = {}
    setStorageSync('classInfo', {})
  },
  setClassInfo(classInfo) {
    // console.log('获取到的', classInfo)
    this.classInfo = classInfo
    setStorageSync('classInfo', classInfo)
  },
  getClassInfo() {
    return this.classInfo || getStorageSync('classInfo')
  },
  setRoleType(roleType) {
    this.roleType = roleType
    setStorageSync('roleType', roleType)
  },
  setOrganizaIdentity(obj) {
    this.organizaIdentity = obj || {}
    setStorageSync('organizaIdentity', obj)
  },
  setToken(token) {
    this.token = token
    setStorageSync('token', token)
  },
  setAccessTokenExpire(accessTokenExpire) {
    this.accessTokenExpire = accessTokenExpire
    setStorageSync('accessTokenExpire', accessTokenExpire)
  },
  // 判断是否token过期
  getIsAccessTokenExpire(unixDateTime) {
    let isTimeOut = false
    const accessTokenExpire = getStorageSync('accessTokenExpire') || this.accessTokenExpire

    // 还有2个小时就要过期了
    if (
      Number(unixDateTime) > 0 &&
      Number(accessTokenExpire) > 0 &&
      Number(unixDateTime) > Number(accessTokenExpire) - 2 * 60 * 60
    ) {
      console.log('accessToken快要过期了, 重新获取下')
      isTimeOut = true
    }
    return isTimeOut
  },

  setChannel(val) {
    this.channel = val
    setStorageSync('channel', val)
  },
  getChannelVal() {
    return this.channel || getStorageSync('channel')
  },
  setOpenId(openId) {
    this.openId = openId
    setStorageSync('openId', openId)
  },
  getOpenId() {
    let openIdVal = getStorageSync('openId') || this.openId
    const token = getStorageSync('token')
    if (openIdVal) {
      return openIdVal
    } else {
      if (token) {
        openIdVal = parseToken(token)?.OpenId
      }
      return openIdVal
    }
  },
  getRoleType() {
    const roleTypeValue = getStorageSync('roleType') || this.roleType
    return {
      roleType: roleTypeValue,
      roleTypeText: this.formatRoleType(roleTypeValue) || this.formatRoleType(getStorageSync('roleType'))
    }
  },
  getOrganizaIdentity() {
    const organizaIdentityValue = getStorageSync('organizaIdentity') || this.organizaIdentity
    return organizaIdentityValue
  },
  formatRoleType(val) {
    const obj = {
      Teacher: '教师',
      Student: '学生'
    }
    return obj[val] + '版'
  },
  setUserInfo(userInfo) {
    this.userInfo = userInfo
    setStorageSync('userInfo', userInfo)
  },
  setIsDidWxAuthBack(val) {
    this.isDidWxAuthBack = val
    setStorageSync('isDidWxAuthBack', val)
  },
  saveAuthH5Code(code) {
    this.authH5Code = code
  },
  savePrivacyPolicyAgree(status) {
    this.privacyPolicyAgree = status
    setStorageSync('privacyPolicyAgree', status)
  },
  getPrivacyPolicyAgree() {
    return getStorageSync('privacyPolicyAgree') || this.privacyPolicyAgree
  },
  setSubscriptionTemplateList(templateList) {
    this.subscriptionTemplateList = templateList
  },
  setSubscriptionTemplateListEdu(templateList) {
    this.subscriptionTemplateListEdu = templateList
  },
  setSubscriptionTemplateListStu(templateList) {
    this.subscriptionTemplateListStu = templateList
  },
  setUploadGradeList(arr) {
    this.uploadGradeList = arr
  },
  setSchoolClassList(arr) {
    this.schoolClassList = arr
  }
})

export default userStore
